/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @Original Author: jin Chen
 * @date: Jul 2, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.visualclassifier.classify.v2.agent;

import edu.psu.geovista.classification.Category;
import edu.psu.geovista.classification.CategoryList;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.Hashtable;
import java.util.Iterator;

public class CategoryTable extends JTable {
    private boolean DEBUG = false;
    private  static final int numOfColumn=4;

    public CategoryTable() {
        //this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF );
        this.setModel(this.generateModel(null) );
        this.setDefaultRenderer(Color.class,new ColorRenderer(true));
        /*TableColumn c2=this.getColumnModel().getColumn(1);
        c2.getHeaderRenderer()
        c2.setWidth(30);
        c2.setMaxWidth(30);
        this.repaint() ;*/


       // this.setPreferredSize(new Dimension(400,300));
    }
    public void setCategory(CategoryList ctgs) {
        TableModel model=this.generateModel(ctgs);
        this.setModel(model);
    }
    private TableModel generateModel(CategoryList ctgs){
       final String[] columnNames = {"ID",
                                      "Color",
                                      "Visible",
                                      "MinMax"};

        Object[][] data = {
                    {new Integer(5), new Color(153, 0, 153), "Snowboarding",  new Boolean(false)},
                    {new Integer(3), new Color(51, 51, 153),"Rowing",  new Boolean(true)},
                    {new Integer(2),  new Color(51, 102, 51),"Chasing toddlers", new Boolean(false)},
                    {new Integer(20), Color.blue, "Speed reading",  new Boolean(true)},
                    {new Integer(7), Color.pink,"Pool",  new Boolean(false)}
             } ;
        if(ctgs!=null)  {
            data=this.extractData(ctgs);
        }

        return new MyTableModel(data, columnNames);


    }
    private Object[][] extractData(CategoryList ctgs){
        Hashtable ctgList=ctgs.getCtgList() ;
        int size=ctgList.size();
        Object[][] data =new Object[size][numOfColumn];
        Iterator iter=ctgList.values().iterator() ;
        int i=0;
        while(iter.hasNext()){
           Category ctg = (Category)iter.next();
           //Integer viewID=new Integer(ctg.getID().intValue() +1);
            Object[] record={ctg.getViewID() ,ctg.getColor(),
                             new Boolean(ctg.isVisible()),Math.round(ctg.getMin()) +"-"+Math.round(ctg.getMax()) } ;

            data[i++] =record;

        }

        return data;
    }

    class MyTableModel extends DefaultTableModel {

    /**
     *  Constructs a <code>DefaultTableModel</code> and initializes the table
     *  by passing <code>data</code> and <code>columnNames</code>
     *  to the <code>setDataVector</code>
     *  method. The first index in the <code>Object[][]</code> array is
     *  the row index and the second is the column index.
     *
     * @param data              the data of the table
     * @param columnNames       the names of the columns
     */
        public MyTableModel(Object[][] data, String[] columnNames) {
           super(data,columnNames);
        }



        /**
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the Boolean column would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            Object o= getValueAt(0, c);
            if (o!=null){
               return o.getClass();
            }
            else{
                return super.getColumnClass(c);
            }
        }

        /**
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 1) {
                return false;
            } else {
                return true;
            }
        }

    }
    class ColorRenderer extends JLabel
                        implements TableCellRenderer {
        Border unselectedBorder = null;
        Border selectedBorder = null;
        boolean isBordered = true;

        public ColorRenderer(boolean isBordered) {
            super();
            this.isBordered = isBordered;
            setOpaque(true); //MUST do this for background to show up.
        }

        public Component getTableCellRendererComponent(
                                JTable table, Object color,
                                boolean isSelected, boolean hasFocus,
                                int row, int column) {
            setBackground((Color)color);
            if (isBordered) {
                if (isSelected) {
                    if (selectedBorder == null) {
                        selectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
                                                  table.getSelectionBackground());
                    }
                    setBorder(selectedBorder);
                } else {
                    if (unselectedBorder == null) {
                        unselectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
                                                  table.getBackground());
                    }
                    setBorder(unselectedBorder);
                }
            }
            return this;
        }
    }

   
}
